package Queue;

public class SequenceQueue<T> {
	private final int DEFAULT_SIZE=5;
	private T[] array;
	private int rear;
	private int front;
	private int size;
	private int count;
	
	{
		rear=0;
		front=0;
		count=0;
	}
	
	public SequenceQueue(int size){
		setSize(size);
		array=(T[])(new Object[this.size]);
	}
	
	private void setSize(int size){
		if (size<=0||size>Integer.MAX_VALUE){
			this.size=this.DEFAULT_SIZE;
			return;
		}
		this.size=size;		
	}
	
	public void push(T value){
		if (!isFull()){
			array[rear]=value;
			++count;
			rear=(rear+1)%size;
		}
	}
	
	public T pop(){
		T value=null;
		if (!isEmpty()){
			value=array[front];
			front=(front+1)%size;
			--count;
		}
		return value;
	}
	
	public boolean isEmpty(){
		return count==0;
	}
	
	public boolean isFull(){
		return count==size;
	}
	
	public int getCount(){
		return count;
	}
	
	
	public String toString(){
		StringBuilder str=new StringBuilder();
		int aFront=front;
		int aCount=count;
		
		while(aCount-->0){
			str.append("->["+array[aFront]+"]");
			aFront=(aFront+1)%size;
		};
		str.delete(0,2);
		return str.toString();
	}
}
